﻿
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using lib.Model;
using lib.DAL;
using System.Linq;
using Common.Unit;

namespace lib.BLL
{
    public partial class MenuInfo_Bll
    {
        /// <summary>
        /// 查询当前可用菜单和菜单权限，生成枚举
        /// </summary>
        /// <returns></returns>
        public IList<MenuInfo_Model> GetCurrentRoleMenu()
        {
            IList<MenuInfo_Model> menuInfoModelList = dal.GetModelList("fdMenuIsEnable=1", "fdMenuName,fdMenuID,FdMenuParentID", "fdMenuSort desc");
            if (menuInfoModelList != null && menuInfoModelList.Count > 0)
            {
                foreach (MenuInfo_Model model in menuInfoModelList)
                {
                    model.menuAccessModel = MenuAccess_Dal.Instance.GetModelList(" fdMeAcCode in( select fdMeCoCode from MenuCode   where fdMeCoMenuID=@fdMeCoMenuID)", "", "", new SqlParameter("@fdMeCoMenuID", model.FdMenuID.Value));
                }
            }
            return menuInfoModelList;
        }

        /// <summary>
        /// 获取当前用户的菜单权限
        /// </summary>
        /// <returns></returns>
        public IList<MenuInfo_Model> GetCurrentUserRoleMenu(int roleId)
        {
            string sql = "select * from MenuInfo where fdMenuID in(select fdRoleMenuID from RoleMenuManage where fdRoleManageID=@fdRoleManageID) and fdMenuIsEnable=1";
            IList<MenuInfo_Model> menuInfoModelList = dal.GetModelList(sql, new SqlParameter("@fdRoleManageID", roleId));
            if (menuInfoModelList != null && menuInfoModelList.Count > 0)
            {
                foreach (MenuInfo_Model model in menuInfoModelList)
                {
                    model.menuAccessModel = MenuAccess_Dal.Instance.GetModelList(" fdMeAcCode in( select fdMeCoCode from MenuCode   where fdMeCoMenuID=@fdMeCoMenuID)", "", "", new SqlParameter("@fdMeCoMenuID", model.FdMenuID.Value));
                }
            }
            return menuInfoModelList;
        }

        /// <summary>
        /// 获取所有下级菜单
        /// </summary>
        /// <param name="parentId"></param>
        /// <returns></returns>
        public IList<MenuInfo_Model> GetParentMenuList(int parentId)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(" with cte(fdMenuID,fdMenuName,fdMenuParentID) as(");
            sql.Append(" select fdMenuID,fdMenuName,fdMenuParentID from MenuInfo where fdMenuID=@fdMenuID union all");
            sql.Append(" select MenuInfo.fdMenuID,MenuInfo.fdMenuName,MenuInfo.fdMenuParentID from cte  inner join MenuInfo on MenuInfo.fdMenuParentID=cte.fdMenuID)");
            sql.Append(" select * from cte");

            return dal.GetModelList(sql.ToString(), new SqlParameter("@fdMenuID", parentId));
        }
        /// <summary>
        /// 保存菜单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SaveMenuInfo(MenuInfo_Model model)
        {
            bool result = false;
            SqlConnection conn;
            using (SqlTransaction trans = SqlHelperWebDAL.CreateSqlTransaction(out conn))
            {
                try
                {
                    if (model.FdMenuID.Value == 0)
                    {
                        model.FdMenuID = dal.Add(model, trans);
                        if (model.FdMenuID.Value != 0)
                        {
                            MenuCode_Model menuCodeModel = new MenuCode_Model()   //默认添加菜单时添加访问的权限
                            {
                                FdMeCoCode = 1000,
                                FdMeCoMenuID = model.FdMenuID
                            };
                            MenuCode_Dal.Instance.Add(menuCodeModel, trans);
                        }
                    }
                    else
                    {
                        dal.Update(trans, model);
                    }
                    trans.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                }
                finally
                {
                    conn.Close();
                }
            }
            return result;
        }
        /// <summary>
        /// 查询当前菜单下的所有操作权限
        /// </summary>
        /// <param name="menuId"></param>
        /// <returns></returns>
        public IList<MenuInfo_Model> GetAllMenuInfoList(int menuId)
        {
            IList<MenuInfo_Model> menuInfoModelList = dal.GetModelList("FdMenuParentID=@FdMenuParentID", "", "fdMenuSort desc", new System.Data.SqlClient.SqlParameter("@FdMenuParentID", menuId));
            if (menuInfoModelList != null)
            {
                foreach (MenuInfo_Model model in menuInfoModelList)
                {
                    //model.menuRoleCodeModel = MenuRoleCode_Dal.Instance.GetModelList("fdRoleCodeMenuID=@fdRoleCodeMenuID", "", "", new SqlParameter("@fdRoleCodeMenuID", model.FdMenuID.Value));
                }
            }
            return menuInfoModelList;
        }
        /// <summary>
        /// 修改菜单状态，总共三级菜单
        /// </summary>
        /// <param name="menuId"></param>
        /// <param name="parentId"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool EditMenuInfoState(MenuInfo_Model menuInfoModel)
        {
            bool result = false;
            SqlConnection conn;
            using (SqlTransaction trans = SqlHelperWebDAL.CreateSqlTransaction(out conn))
            {
                try
                {
                    //查询递归出当前MenuID的所有下级ID
                    List<int> temMenuId = new List<int>();
                    IList<MenuInfo_Model> menuInfoModelList = GetParentMenuList(menuInfoModel.FdMenuID.Value);
                    if (menuInfoModelList != null)
                    {
                        foreach (MenuInfo_Model model in menuInfoModelList)
                        {
                            temMenuId.Add(model.FdMenuID.Value);
                        }
                    }
                    dal.Update(trans, "FdMenuIsEnable=@FdMenuIsEnable", "fdMenuID in(" + temMenuId.SeperateToString(",") + ")",
                          new System.Data.SqlClient.SqlParameter("@FdMenuIsEnable", menuInfoModel.FdMenuIsEnable));

                    /*****以及禁用所有用户在该菜单的权限数据*****/
                    trans.Commit();
                    result = true;

                }
                catch (Exception ex)
                {
                    trans.Rollback();
                }
                finally
                {
                    conn.Close();
                }
            }
            return result;
        }
        /// <summary>
        /// 删除菜单，总共三级菜单
        /// </summary>
        /// <param name="menuInfoModel"></param>
        /// <returns></returns>
        public bool DelMenuInfo(MenuInfo_Model menuInfoModel)
        {
            bool result = false;
            SqlConnection conn;
            using (SqlTransaction trans = SqlHelperWebDAL.CreateSqlTransaction(out conn))
            {
                try
                {
                    //查询递归出当前MenuID的所有下级ID，并删除
                    List<int> temMenuId = new List<int>();
                    IList<MenuInfo_Model> menuInfoModelList = GetParentMenuList(menuInfoModel.FdMenuID.Value);
                    if (menuInfoModelList != null)
                    {
                        foreach (MenuInfo_Model model in menuInfoModelList)
                        {
                            temMenuId.Add(model.FdMenuID.Value);
                        }
                    }
                    //删除当前菜单和下级所有的菜单
                    dal.Delete("fdMenuID in(" + temMenuId.SeperateToString(",") + ")", trans);
                    //删除菜单之后，继续删除菜单的权限
                    MenuCode_Dal.Instance.Delete("FdMeCoMenuID in(" + temMenuId.SeperateToString(",") + ")", trans);
                    /*****以及禁用所有用户在该菜单的权限数据*****/
                    RoleMenuManage_Dal.Instance.Delete("fdRoleMenuID in (" + temMenuId.SeperateToString(",") + ")", trans);

                    trans.Commit();
                    result = true;
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                }
                finally
                {
                    conn.Close();
                }
            }
            return result;
        }
    }
}

